import React, { Component, useState, useEffect } from 'react';
import EditTemplate from '@/components/EditTemplate';
import { connect } from 'dva';

const UserInfo = ({ dispatch, userInfo }) => {
  const [filters, setFilters] = useState([]);
  const filters1 = [
    {
      title: '个人中心',
      list: [
        {
          key: 'orgUnitName',
          type: 'text',
          span: 24,
          label: '单位名称',
        },
        {
          key: 'departmentName',
          type: 'text',
          span: 24,
          label: '所在部门',
        },
        {
          key: 'roleName',
          type: 'text',
          span: 24,
          label: '我的角色',
        },
        {
          key: 'nickname',
          type: 'text',
          span: 24,
          label: '姓名',
        },
      ],
    },
  ];
  const filters2 = [
    {
      title: '个人中心',
      list: [
        {
          key: 'orgUnitName',
          type: 'text',
          span: 24,
          label: '单位名称',
        },
        {
          key: 'roleName',
          type: 'text',
          span: 24,
          label: '我的角色',
        },
      ],
    },
  ];
  const saveUrl = '/api/account/v1/updatePassword';
  const [detail, setdetail] = useState([]);
  useEffect(() => {
    dispatch({
      type: '$request/post',
      payload: {
        url: '/api/account/v1/getUserInfo',
      },
    }).then(res => {
      if (~res.roleName.indexOf('业主单位')) {
        setFilters(filters2);
      } else {
        setFilters(filters1);
      }
      setdetail(res);
    });
  }, []);

  return (
    <div>
      <EditTemplate
        filters={filters}
        initialValues={detail}
        updateUrl={saveUrl}
      />
    </div>
  );
};
export default connect(state => state.auth)(UserInfo);
